<template>
  <div class="order-list">
    <el-table :data="orders" style="width: 100%">
      <el-table-column prop="id" label="订单编号" width="180" />
      <el-table-column label="商品信息">
        <template #default="{ row }">
          <div v-for="item in row.items" :key="item.drugId" class="order-item">
            <img :src="item.drug.image" class="drug-image">
            <div class="item-info">
              <p>{{ item.drug.name }}</p>
              <p>数量：{{ item.quantity }}</p>
              <p>单价：¥{{ item.price }}</p>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="total" label="总价" width="120">
        <template #default="{ row }">
          ¥{{ row.total }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120" />
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button
            v-if="row.status === '未支付'"
            type="primary"
            size="small"
            @click="handlePay(row)"
          >
            支付
          </el-button>
          <el-button
            v-if="row.status === '未支付'"
            type="danger"
            size="small"
            @click="handleCancel(row)"
          >
            取消
          </el-button>
          <el-button
            v-if="row.status === '已支付'"
            type="success"
            size="small"
            @click="handleComplete(row)"
          >
            确认收货
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { defineProps, defineEmits } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { updateOrder } from '../api'

export default {
  name: 'OrderList',
  props: {
    orders: {
      type: Array,
      required: true
    }
  },
  emits: ['refresh'],
  setup(props, { emit }) {
    const handlePay = async (order) => {
      try {
        await ElMessageBox.confirm('确认支付该订单吗？')
        await updateOrder(order.id, { status: '已支付' })
        ElMessage.success('支付成功')
        emit('refresh')
      } catch (error) {
        if (error !== 'cancel') {
          ElMessage.error('支付失败')
        }
      }
    }

    const handleCancel = async (order) => {
      try {
        await ElMessageBox.confirm('确认取消该订单吗？')
        await updateOrder(order.id, { status: '已取消' })
        ElMessage.success('订单已取消')
        emit('refresh')
      } catch (error) {
        if (error !== 'cancel') {
          ElMessage.error('取消失败')
        }
      }
    }

    const handleComplete = async (order) => {
      try {
        await ElMessageBox.confirm('确认收到商品了吗？')
        await updateOrder(order.id, { status: '已完成' })
        ElMessage.success('订单已完成')
        emit('refresh')
      } catch (error) {
        if (error !== 'cancel') {
          ElMessage.error('确认收货失败')
        }
      }
    }

    return {
      handlePay,
      handleCancel,
      handleComplete
    }
  }
}
</script>

<style scoped>
.order-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.order-item:last-child {
  border-bottom: none;
}

.drug-image {
  width: 60px;
  height: 60px;
  object-fit: cover;
  margin-right: 15px;
}

.item-info {
  flex: 1;
}

.item-info p {
  margin: 5px 0;
  color: #666;
}
</style> 